package com.winway.sixbook.service;

import java.util.ArrayList;
import java.util.List;
import com.winway.sixbook.bean.SeedData;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class SeedManager {

	private static final String TAG = "SeedManager";
	private static final String DB_NAME = "sixbook";
	private static final String TABLE_NAME = "seeds";
	public static final String  ID = "_id";
	public static final String SEED_NAME = "seed_name"; 

	private static final int DB_VERSION = 1;
	private Context mContext = null;
	private SQLiteDatabase mSQLiteDatabase = null;
	private DataBaseManagementHelper mDatabaseHelper = null;

	public SeedManager(Context context) {
		mContext = context;
		Log.i(TAG, "SeedManager construction!");
	}

	public void openDataBase() throws SQLException {
		mDatabaseHelper = DataBaseManagementHelper.getInstance(mContext);
		mSQLiteDatabase = mDatabaseHelper.getWritableDatabase();
	}

	public void closeDataBase() throws SQLException {
		mDatabaseHelper.close();
	}

	public long insertSeedData(SeedData seedData) {
		String seedName=seedData.getSeedName(); 
		ContentValues values = new ContentValues();
		values.put(SEED_NAME, seedName); 
		return mSQLiteDatabase.insert(TABLE_NAME, ID, values);
	}
 

	public Cursor fetchUserData(int id) throws SQLException {
		Cursor mCursor = mSQLiteDatabase.query(false, TABLE_NAME, null, ID + "=" + id, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public Cursor fetchAllSeedDatas() {

		return mSQLiteDatabase.query(TABLE_NAME, null, null, null, null, null, null);
	}
	
	public String[] fetchAllSeedNames() {
		
		List<String> list = new ArrayList<String>();
		list.add("所有类型");
		SeedData seedData = null ;
		Cursor cursor = mSQLiteDatabase.query(TABLE_NAME, null, null, null, null, null, null);
		if(null != cursor){
	            if(cursor.moveToFirst() ){
	                do{
	                	list.add(cursor.getString(cursor.getColumnIndex("seed_name")));
	                }while( cursor.moveToNext());
	            }
		 }
		return  list.toArray(new String[list.size()]) ;
	}
	
	public List<String> fetchListNames() {
		
		List<String> list = new ArrayList<String>();
		SeedData seedData = null ;
		Cursor cursor = mSQLiteDatabase.query(TABLE_NAME, null, null, null, null, null, null);
		if(null != cursor){
	            if(cursor.moveToFirst() ){
	                do{
	                	list.add(cursor.getString(cursor.getColumnIndex("seed_name")));
	                }while( cursor.moveToNext());
	            }
		 }
		return  list ;
	}

	public boolean deleteSeedData(int id) {

		return mSQLiteDatabase.delete(TABLE_NAME, ID + "=" + id, null) > 0;
	}
 
	public int findSeedByName(String seedName){
		int result=0; 
		Cursor mCursor=mSQLiteDatabase.rawQuery("select * from seeds where seed_name='"+seedName+"'", null);
		if(mCursor!=null){
			result=mCursor.getCount();
			mCursor.close();
		}
		return result;
	}

	public String getStringByColumnName(String columnName, int id) {
		Cursor mCursor = fetchUserData(id);
		int columnIndex = mCursor.getColumnIndex(columnName);
		String columnValue = mCursor.getString(columnIndex);
		mCursor.close();
		return columnValue;
	}
	 
	public List<SeedData> fetchAllSeedList() {
		
		List<SeedData> list = new ArrayList<SeedData>();
		SeedData seedData = null ;
		Cursor cursor = mSQLiteDatabase.query(TABLE_NAME, null, null, null, null, null, null);
		if(null != cursor){
	            if(cursor.moveToFirst() ){
	                do{
	                	seedData = new SeedData();
	                	seedData.setId(cursor.getString(cursor.getColumnIndex("_id")));
	                	seedData.setSeedName(cursor.getString(cursor.getColumnIndex("seed_name")));
	                	list.add(seedData);
	                }while( cursor.moveToNext());
	            }
		 }
		return  list ;
	}
	
	public boolean updateSeedData(String id,String seedname) {
		ContentValues values = new ContentValues();
		values.put(SEED_NAME, seedname);
		return mSQLiteDatabase.update(TABLE_NAME, values, ID + "=" + id, null) > 0;
	}
	
	public int findZuiHaoCount(String datestr){
		int result=0; 
		Cursor mCursor=mSQLiteDatabase.rawQuery("select * from gggrecord where type_id =0 and add_time='"+datestr+"'" , null);
		if(mCursor!=null){
			result=mCursor.getCount();
			mCursor.close();
			Log.i(TAG,"findTodayQyCount , result="+result);
		}
		return result;
	}
	
	public String findZuiHaoId(String datestr){
		String qyid = "0";
		Cursor mCursor=mSQLiteDatabase.rawQuery("select * from gggrecord where type_id =0 and add_time='"+datestr+"'" , null);
		if (mCursor != null) {
			mCursor.moveToFirst();
			qyid = mCursor.getString(mCursor.getColumnIndex("_id"));
		}
		return qyid;
	}
}
